package com.ls.data.controller;

import com.alibaba.fastjson.JSONObject;
import com.ls.data.service.*;
import com.ls.interPojo.PhoneAttribution;
import com.ls.pojo.CustInter;
import com.ls.pojo.Inter;
import com.ls.pojo.InterThirdparty;
import com.ls.utils.HttpClientUtils;
import com.ls.utils.Result;
import com.ls.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 段成
 * @date 2020/1/9 - 18:46
 */
@RestController
@RequestMapping(value = "/phoneAttribution")
public class PhoneAttributionController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private InterfaceService interfaceService;

    @Autowired
    private CustomerInterfaceService customerInterfaceService;

    @Autowired
    private InterThirdpartyService interThirdpartyService;

    @Autowired
    private PhoneAttributionService phoneAttributionService;

    @GetMapping("/getData")
    public Result getData(@RequestParam(name = "phone",defaultValue = "") String phone, @RequestParam(name = "appkey",defaultValue = "") String appkey){
        if("".equals(appkey) || null == appkey || "".equals(phone) || null == phone){
            return new Result(false, StatusCode.PARAMETER_IS_EMPTY,"参数为空");
        }

        try {
            //  通过地址查询接口id
            StringBuffer requestURL = null;
            requestURL = request.getRequestURL();
            Inter inter = interfaceService.getInterfaceByURL(requestURL.toString());
            if(inter == null){
                return new Result(false, StatusCode.ERROR,"接口不存在");
            }

            CustInter custInter = customerInterfaceService.getAppkeyByAppkey(appkey,inter.getId());

            // 为null 则没有这条记录  appkey不存在或错误
            if(custInter == null){

                return new Result(false, StatusCode.ERROR,"appkey不存在或错误");
            }else {
                //查本地
                PhoneAttribution phoneAttribution = phoneAttributionService.getDataByPhone(phone);
                if(phoneAttribution!=null){
                    System.out.println("此次查询为本地查询");
                    return new Result(true, StatusCode.OK,"本地查询成功",phoneAttribution,null);
                }else {
                    System.out.println("本地没有则第三方查询  主1从2辅3");
                    //本地没有则第三方查询  主1从2辅3
                    InterThirdparty interThirdparty1 = interThirdpartyService.getListByInterfaceId(inter.getId(),1);
                    //沉淀数据
                    PhoneAttribution insertData = this.StitchingAddress(interThirdparty1, phone);


                    return new Result(true, StatusCode.OK,"第三方查询成功",insertData,null);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败出现异常");
        }


    }



    private PhoneAttribution StitchingAddress(InterThirdparty interThirdparty, String phone) {
        //请求第三方数据
        StringBuffer reqUrl = new StringBuffer();
        reqUrl.append(interThirdparty.getThirdpartyUrl());
        reqUrl.append("?phone=");
        reqUrl.append(phone);
        reqUrl.append("&key=");
        reqUrl.append(interThirdparty.getAppkey());
        System.out.println(reqUrl.toString());
        JSONObject jsonObject = HttpClientUtils.httpGet(reqUrl.toString());
        Map map = (Map)jsonObject.get("result");
        String province = map.get("province").toString();
        String city = map.get("city").toString();
        String areacode = map.get("areacode").toString();
        String zip = map.get("zip").toString();
        String company = map.get("company").toString();
        String card = map.get("card").toString();
        //沉淀数据
        PhoneAttribution insertData = new PhoneAttribution();
        insertData.setCity(city);
        insertData.setAreacode(areacode);
        insertData.setCard(card);
        insertData.setCompany(company);
        insertData.setPhone(phone);
        insertData.setProvince(province);
        insertData.setZip(zip);
        phoneAttributionService.insertData(insertData);
        System.out.println(jsonObject.toString());
        return insertData;
    }

}
